package Dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Negocio.Ganancia;
import Persistencia.HibernateUtil;

public class GananciaDao {
	
	private static GananciaDao instancia;
	private static SessionFactory sf = null;
	
	public static GananciaDao getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new GananciaDao();
		}
		return instancia;
	}
	
	public void setGanancia(Ganancia gcia){
		Session session = null;
		try {
			session = sf.openSession();			
			session.beginTransaction();
			session.save(gcia);
			session.getTransaction().commit();
			session.flush();
			session.close();
		} catch (Exception e) {
			if(session != null){
				session.getTransaction().rollback();	
				session.flush();
				session.close();
			}			
		}
	}
	
	public float getGananciActual(){
		Session session = sf.openSession();
		session.beginTransaction();
		List<Float> list = session.createQuery("select porcentaje from Ganancia g where g.id = (select max(id) from Ganancia)").list();
		float porc = list.iterator().next();
		session.flush();
		session.getTransaction().commit();
		session.close();
		return porc;
	}
	
	
	

}


